'use strict';

const Controller = require('egg').Controller;

class CategoryController extends Controller {
  // 列表
  async index() {
    const { ctx, app } = this;

    let data = await ctx.page('Category', {}, {})
    data = JSON.parse(JSON.stringify(data))
    console.log(data)

    await ctx.renderTemplate({
        title: "分类列表",
        tempType: "table",
        table: {
            // 按钮
            buttons: {
                // 新增操作
                add: "/admin/category/create"
            },
            // 表头
            columns: [{
                title: "ID",
                fixed: 'center',
                key: "id"
            }, {
                title: "名称",
                fixed: 'left',
                key: "title"
            }, {
                title: "创建时间",
                fixed: 'center',
                width: 180,
                key: "created_time"
            }, {
                title: "排序",
                fixed: 'center',
                key: "order"
            }, {
                title: "操作",
                width: 200,
                fixed: 'center',
                action: {
                    edit: function (id) {
                        return `/admin/category/edit/${id}`
                    },
                    delete: function (id) {
                        return `/admin/category/delete/${id}`
                    },
                }
            }]
        },
        data
    })
}
// 创建分类表单
async create() {
    const { ctx, app } = this;
    await ctx.renderTemplate({
        // 页面标题
        title: "添加分类",
        // 模板类型 form表单，table表格分页
        tempType: "form",
        // 表单配置
        form: {
            // 提交地址
            action: "/admin/category",
            // 字段配置
            fields: [{
                label: "类型",
                type: "select",
                name: "type",
                field: "name",
                options: [{
                    name:"文章",
                    id: 1
                },{
                    name:"图片",
                    id: 2
                },
                {
                    name:"单页",
                    id: 3
                }],
                defValue: 1,
                placeholder: "分类类型"
            }, {
                label: "名称",
                type: "text",
                name: "title",
                placeholder: "名称"
            }, {
                label: "封面图",
                type: "file",
                name: "thumb",
                placeholder: "封面图"
            }, {
                label: "关键词",
                type: "text",
                name: "keywords",
                placeholder: "关键词"
            }, {
                label: "描述",
                type: "text",
                name: "description",
                placeholder: "描述"
            }, {
                label: "排序",
                type: "number",
                name: "order",
                defValue: 0,
                placeholder: "排序"
            }]
        },
        // 新增成功跳转路径
        successUrl: "/admin/category"
    });
}

// 创建分类逻辑
async save() {
    const { ctx, app } = this;

    ctx.validate({
        type: {
            type: "int",
            required: true,
            desc: "类型"
        },
        title: {
            type: "string",
            required: true,
            desc: "名称"
        },
        thumb: {
            type: "string",
            required: false,
            desc: "封面图"
        },
        keywords: {
            type: "string",
            required: false,
            desc: "关键词"
        },
        description: {
            type: "string",
            required: false,
            desc: "描述"
        },
        order:{
            type:"int",
            required:true,
            defValue:0,
            desc:"排序"
        },
    })

    let { type, title, thumb, keywords, description, order} = ctx.request.body;

    if (await app.model.Category.findOne({
        where: {
            title
        }
    })) {
        return ctx.apiFail('该分类已存在');
    }

    let category = await app.model.Category.create({
        type, title, thumb, keywords, description,order
    })

    ctx.apiSuccess(category)
}

// 编辑表单页
async edit() {
    const { ctx, app } = this
    const id = ctx.params.id

    let data = await app.model.Category.findByPk(id);
    if (!data) {
        return await ctx.pageFail('该记录不存在')
    }

    data = JSON.parse(JSON.stringify(data))

    await ctx.renderTemplate({
        id,
        title: "修改分类",
        tempType: "form",
        form: {
            // 提交地址
            action: "/admin/category/" + id,
            // 字段配置
            fields: [{
                label: "类型",
                type: "select",
                name: "type",
                field: "name",
                options: [{
                    name:"文章",
                    id: 1
                },{
                    name:"图片",
                    id: 2
                },
                {
                    name:"单页",
                    id: 3
                }],
                placeholder: "分类类型"
            }, {
                label: "名称",
                type: "text",
                name: "title",
                placeholder: "名称"
            }, {
                label: "封面图",
                type: "file",
                name: "thumb",
                placeholder: "封面图"
            }, {
                label: "关键词",
                type: "text",
                name: "keywords",
                placeholder: "关键词"
            }, {
                label: "描述",
                type: "text",
                name: "description",
                placeholder: "描述"
            }, {
                label: "排序",
                type: "number",
                name: "order",
                placeholder: "排序"
            }],
            // 默认值
            data
        },
        // 修改成功跳转路径
        successUrl: "/admin/category"
    })
}

// 更新逻辑
async update() {
    const { ctx, app } = this;
    ctx.validate({
        id: {
            type: "int",
            required: true
        },
        type: {
            type: "int",
            required: true,
            defValue:1,
            desc: "类型"
        },
        title: {
            type: "string",
            required: true,
            desc: "名称"
        },
        thumb: {
            type: "string",
            required: false,
            desc: "封面图"
        },
        keywords: {
            type: "string",
            required: false,
            desc: "关键词"
        },
        description: {
            type: "string",
            required: false,
            desc: "描述"
        },
        order:{
            type:"int",
            required:true,
            defValue:0,
            desc:"排序"
        },
    })

    let id = ctx.params.id
    let { type, title, thumb, keywords, description, order} = ctx.request.body

    let category = await app.model.Category.findOne({
        where: {
            id
        }
    })
    if (!category) {
        return ctx.apiFail('该记录不存在')
    }

    const Op = app.Sequelize.Op

    category.type = type
    category.title = title
    category.thumb = thumb
    category.keywords = keywords
    category.description = description
    category.order = order

    ctx.apiSuccess(await category.save())

}

// 删除
async delete() {
    const { ctx, app } = this;
    const id = ctx.params.id
    await app.model.Category.destroy({
        where: {
            id
        }
    })
    ctx.toast('删除成功', 'success')
    ctx.redirect('/admin/category')
}
}

module.exports = CategoryController;
